#ifndef GPICTURE_H
#define GPICTURE_H

#include <iostream>
#include <vector>
#include <string>
#include "Picture.h"

using namespace std;

/**
* This class will display a Picture.
*
* Adapted by Alfredo Paz from Haskell (Heijltjes, Walder)
*/
class GPicture{
private:

  Picture *pic;

  //Dibujar linea, s=fila de imagen, y=num

  void parseLine(int y, string s){
    if(s.size() == 0) return;
    int xi = 0, xf = 0;
    char c = s[xi];
    while( xf < s.size() ){
      if( c == ' ' && c != s[xf]){//end of spaces
        c = s[xf];
        xi = xf;
      }else if( c != s[xf] ){ //end of pattern
        //draw a line
        parseChar(c); glLineWidth(1);
        glBegin(GL_LINES);
          glVertex2i(xi,y); glVertex2i(xf,y);
        glEnd();

        //start a new pattern
        c = s[xf];
        xi = xf;
      }
      xf++;
    }
    if( c != ' ' ){
        //draw a line
        parseChar(c); glLineWidth(1); 
        glBegin(GL_LINES);
          glVertex2i(xi,y); glVertex2i(xf,y);
        glEnd();
    }
  }

  //color
  void parseChar(char c){
    switch (c){
      case '_': glColor3f(0.75f, 0.75f, 0.75f); break;//Light gray
      case '=': glColor3f(0.5f, 0.5f, 0.5f); break;   //Gray
      case '.': glColor3f(1.0f, 1.0f, 1.0f); break;   //White
      case '@': glColor3f(0, 0, 0); break;            //Black
      case '#': glColor3f(0.25f, 0.25f, 0.25f); break;//Dark gray
      default: glColor3f(0, 0, 0); break;             //Black
    }
  }
public:
  /**
  * Constructor for GPicture
  * @param pic the picture to display.
  */
  GPicture(Picture *pic){
    this->pic = pic;
  }

  /**
  * Draw the Picture
  */
  void paint(){
    vector<string> img = pic->getImg();
    int n = img.size();
    for(int i = 0; i < n; i++)
      parseLine(i, img[n - 1 - i]); //desde la ultima fila
    
  }
};
#endif
